package com.levi.linkedlist;

/**
 * @author shentong
 * @date 2021/8/23 22:04
 * @Description 双向链表的操作实现
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        var node1 = new DoubleHeroNode(1, "路飞", "海贼王");
        var node2 = new DoubleHeroNode(2, "索隆", "剑豪");
        var node3 = new DoubleHeroNode(3, "山治", "老色批");
        var node4 = new DoubleHeroNode(4, "娜美", "航海士");
        var nodeList = new DoubleHeroNodeList();
        nodeList.add(node1);
        nodeList.add(node2);
        nodeList.add(node3);
        nodeList.add(node4);
        System.out.println("遍历链表(原始链表):--------");
        nodeList.list();
        System.out.println("修改后的链表:---------");
        nodeList.update(4,"乔巴","神医");
        nodeList.list();
        Integer a = 2;
        System.out.printf("删除第%d后剩余的链表节点是:----------\n",a);
        nodeList.delete(a);
        nodeList.list();
    }
}

class DoubleHeroNodeList{
    //头节点
    private final DoubleHeroNode head = new DoubleHeroNode(0, "", "");

    public DoubleHeroNode getHead() {
        return head;
    }

    /**
     * 往双向链表的尾部添加节点
     */
    public void add(DoubleHeroNode node) {
        var temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = node;
        node.pre = temp;
    }

    /**
     * 遍历
     */
    public void list() {
        //如果链表为空 直接返回
        if (head.next == null) {
            return;
        }
        var temp = head.next;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }

    /**
     * 根据编号修改,也需要遍历找到对应的node
     * @param no
     * @param name
     * @param nickName
     */
    public void update(Integer no, String name, String nickName) {
        //如果链表为空 直接返回
        if (head.next == null) {
            return;
        }
        var temp = head.next;
        var flag = false;
        while (temp != null) {
            if (temp.no.equals(no)) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = name;
            temp.nickName = nickName;
        }else System.out.printf("没有找到编号=%d的节点",no);
    }

    /**
     * 根据no删除对应的节点
     * 1.遍历找到对应的节点temp
     * 2.把temp.pre.next = temp.next , temp.next.pre = temp.pre
     * @param no
     */
    public void delete(Integer no) {
        //如果链表为空 直接返回
        if (head.next == null) {
            return;
        }
        var flag = false;
        var temp = head.next;
        while (temp != null) {
            if (temp.no.equals(no)) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            if (temp.pre == null) {
                temp.next.pre = null;

            } else if (temp.next == null) {
                temp.pre.next = null;
            }else {
                temp.pre.next = temp.next;
                temp.next.pre = temp.pre;
            }

        }else System.out.printf("没有找到编号=%d的节点",no);
    }
}
class DoubleHeroNode {
    public Integer no;
    public String name;
    public String nickName;
    public DoubleHeroNode pre;
    public DoubleHeroNode next;

    public DoubleHeroNode(Integer no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}